<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

/*
        var user = {
            name:"iwen",
            age:20
        }

        console.log(user.name);
        console.log(user.age);

        //利用解构赋值方式简化

        const{name,age} = user;
        console.log(name,age);
*/

        //温馨提示
        //对象的属性没有次序，变量必须与属性同名，才能取到正确的值
        // let{age,name} = {name:"iwen",age:21};
        // console.log(age);//21
        
        let{sex,age,name} = {name:"iwen",age:21};
        console.log(sex);//undefined

        //对象的解构赋值，可以很方便地将现有对象的方法，赋值到某个变量
        let{random,floor} = Math;
        let{log} = console;
        log(random());//可以直接输出随机数了

        //注意事项，如果将一个已经声明的变量用于解构赋值，必须非常小心
        /*
        let hello = "Hello";
        let{hello} = {hello:"Hello"};//报错
        */
        let hello = "Hello";
        ({hello}) = {hello:"Hello"};//正确
        
        
        
        
    </script>
</body>
</html>